"use strict";

$(function () {
  var token = localStorage.getItem("token");
  var id = localStorage.getItem("id");
  var username = localStorage.getItem("username"); //二维码

  if (!token && !id) {
    //表示你还未登录
    $(".islogin").hide();
    $(".notlogin").show();
  } else {
    $(".islogin").show();
    $(".notlogin").hide();
    $(".islogin_name").text(username);
  }

  $(".islogin_exit").click(function () {
    localStorage.removeItem("token");
    localStorage.removeItem("id");
    localStorage.removeItem("username");
    location.reload();
  });

  if (token && id) {
    $.get("http://jx.xuzhixiang.top/ap/api/cart-list.php", {
      id: id
    }, function (res) {
      $(".ci-count").text(res.data.length);
    });
  }

  $(".islogin").hover(function () {
    $(".islogin_dorp").show();
  }, function () {
    $(".islogin_dorp").hide();
  });
  $("#ttbar-myjd").hover(function () {
    $(".dropdown-layer").show().css("z-index", 4);
  }, function () {
    $(".dropdown-layer").hide();
  });
  $("#ttbar-myjd1").hover(function () {
    $(".dropdown-layer1").show().css("z-index", 4);
  }, function () {
    $(".dropdown-layer1").hide();
  }); //调用接口

  $.get("http://jx.xuzhixiang.top/ap/api/productlist.php", {
    uid: 74364,
    pagesize: 20
  }, function (res) {
    var str = "";
    res.data.forEach(function (item) {
      str += "\n        <li class=\"commodity_item clearfix\" data-id=\"".concat(item.pid, "\">\n          <a class=\"c_item_img\">\n            <img\n              src=\"").concat(item.pimg, "\"\n              alt=\"\"\n            />\n          </a>\n          <p class=\"c_item\">\uFFE5").concat(item.pprice, "</p>\n          <a href=\"\" class=\"c_pdesc\">").concat(item.pdesc, "</a\n          >\n          <strong class=\"c_commit\"><span>1\u4E07+</span>\u6761\u8BC4\u4EF7</strong>\n          <span class=\"c_J_icon\">\n            <a>\u4EAC\u4E1C\u81EA\u8425\u5E97</a>\n            <i title=\"\u8054\u7CFB\u5BA2\u670D\"></i>\n          </span>\n          <em class=\"J_zy\">\u81EA\u8425</em>\n          <div class=\"c_add_cart\" data-id=\"").concat(item.pid, "\">\n            <i></i>\n            \u52A0\u5165\u8D2D\u7269\u8F66\n          </div>\n        </li>");
    });
    $(".commodity_list").html(str);
    $(".commodity_item").click(function (e) {
      e.stopPropagation();
      localStorage.setItem("pid", $(this).attr("data-id"));
      location.href = "./datails.html";
    });
    $(".c_add_cart").click(function (e) {
      e.stopPropagation();

      if (!token && !id) {
        alert("请先登录");
        return;
      } else {
        $.get("http://jx.xuzhixiang.top/ap/api/add-product.php",{
          pnum: 1,
          pid: $(this).attr("data-id"),
          uid: id
        }, function (data) {
          alert("加入购物车成功");

          if (token && id) {
            $.get("http://jx.xuzhixiang.top/ap/api/cart-list.php", {
              id: id
            }, function (res) {
              $(".ci-count").text(res.data.length);
            });
          }
        });
      }
    });
  });
  $(".p_num>.prev").click(function () {
    $(".p_num").find(".active").removeClass("active");
    $(this).addClass("active");
  });
});